/*
 * @Descripttion: 
 * @msg: 告警处理文件：用在 首页告警处理 和 消息页面的告警处理 两个页面中
 * @version: 
 * @Author: yuanliutech
 * @Date: 2022-11-22 10:08:14
 * @LastEditors: yuanliutech@lwj
 * @LastEditTime: 2023-01-31 16:01:35
 */
const app = getApp()
import {
    showMsg
} from '../../../utils/config.js'
import {
    validateEmoji,
    validateSpecial
} from '../../../utils/validate.js'
import {
    reqAlarmHandling,
    reqAlarmHandlingResult
} from '../../../request/api'
module.exports = Behavior({
    data: {
        showAlarmHandle: false,
        beforeClose: {},
        alarmItemInfo: {},
        form: {
            factor: '',
            remarks: '',

        },
        showFactor: false,
        factorList: [{
                id: 0,
                text: '核酸检测'
            },
            {
                id: 1,
                text: '运送生活物资'
            },
            {
                id: 2,
                text: '垃圾清理'
            },
            {
                id: 3,
                text: '报警'
            },
            {
                id: 4,
                text: '送药'
            },
            {
                id: 5,
                text: '测温'
            },
            {
                id: 6,
                text: '设备测试'
            },
        ],

    },
    methods: {
        // 点击取消/确定回调前
        setAlarmHandleBeforeClose() {
            this.setData({
                beforeClose: (action) => this.alarmConfirm(action),
            })
        },
        // 点击展示告警处理
        clickShowAlarmHandle(e) {
            if (e.currentTarget.dataset.item) {
                this.setData({
                    showAlarmHandle: true,
                    alarmItemInfo: e.currentTarget.dataset.item
                })
            } else if (e.currentTarget.dataset.id) {
                this.reqAlarmHandlingResult(e.currentTarget.dataset.id)
                this.setData({
                    showAlarmHandle: true,
                    'form.id': '1111', //随机给的一个值，目的是让form.id快速有一个值
                })
            }
        },
        // 获取告警处理结果
        async reqAlarmHandlingResult(id) {
            const res = await reqAlarmHandlingResult(id)
            if (res.code !== 20000) return showMsg(res.message)
            let {
                row
            } = res.data
            this.setData({
                'form.id': row.id,
                'form.factor': row.alarmFactor,
                'form.remarks': row.remarks,
            })
        },
        // 弹出告警因素
        clickFactor() {
            this.setData({
                showFactor: true
            })
        },
        // 隐藏告警因素
        factorClose() {
            this.setData({
                showFactor: false
            })
        },
        // 确定告警因素
        factorConfirm(e) {
            const {
                value,
                index
            } = e.detail;
            this.setData({
                "form.factor": value.text,
                showFactor: false,
            })
        },

        // 获取备注内容
        remarksChange(e) {
            this.data.form.remarks = validateEmoji(e.detail.value.replace(/\s+/g, ''))
            this.setData({
                "form.remarks": this.data.form.remarks
            })
        },

        async alarmConfirm(action) {
            if (action === 'confirm') {
                let {
                    factor,
                    remarks,
                } = this.data.form
                let {
                    alarmItemInfo
                } = this.data
                if (!factor) {
                    showMsg("请选择告警因素")
                    return false
                }
                if (!remarks) {
                    showMsg("请填写处理备注")
                    return false
                }
                let params = {
                    orgId: app.globalData.orgId ==1?'':app.globalData.orgId,
                    id: alarmItemInfo.id,
                    name: alarmItemInfo.name,
                    address: alarmItemInfo.address,
                    imei: alarmItemInfo.imei,
                    deviceType: alarmItemInfo.deviceType,
                    spaceId: alarmItemInfo.spaceId,
                    parkId: alarmItemInfo.parkId,
                    alarmType: alarmItemInfo.alarmType,
                    createTime: alarmItemInfo.createTime,
                    deviceType:alarmItemInfo.deviceType,
                    alarmFactor: factor,
                    remarks,
                }
                const res = await reqAlarmHandling(params)
                if (res.code !== 20000) {
                    showMsg(res.message)
                    return
                }
                showMsg("处理成功", 'success')
                this.alarmCancel()
                this.getInitDataList()
            } else {
                this.alarmCancel()
                setTimeout(() => {
                    this.setData({
                        "form.factor": '',
                        "form.remarks": '',
                        'form.id': ''
                    })
                }, 500)
            }
        },
        alarmCancel() {
            this.setData({
                showAlarmHandle: false
            })
        },
    }
})